import heapq
class Solution:
    def thirdMax(self, nums) -> int:
        nums = set(nums)
        n = 3
        pq = []
        for i in nums:
            if len(pq) < n:
                heapq.heappush(pq, i)
            elif pq[0] < i:
                heapq.heapreplace(pq, i)
        return pq[0] if len(pq)==n else max(pq)



if __name__ == '__main__':
    so = Solution()
    num = [2, 2, 3, 1]
    r = so.thirdMax(num)
    print(r)
